var START_CARDS = 8;
var NUM_ROUNDS = 9;

var TURN_START = 0;
var TURN_PLAY = 1;
var TURN_END = 2;

ExtGames.MayIGame = function(config) {
	
	ExtGames.MayIGame.superclass.constructor.call(this, config);

};

Ext.extend(ExtGames.MayIGame, ExtGames.Game, {
	

	nextTurn: function() {
		ExtGames.MayIGame.superclass.nextTurn.call(this);
		
		if (Ext.getCmp('mayIButton').pressed == true) {
		  Ext.getCmp('mayIButton').toggle(false);
	  	}
	},

	mayIRecieved: function(requestId) {
	  	if (this.mayI == null) {
	  		this.mayI = requestId;
	  	}
	  	else {
	  		var opp = util.getOpponents();
	  		for (var i = 0; i < opp.length; i++) {
	  			if (opp[i].id == this.mayI) {
	  				return;
	  			}
	  			if (opp[i].id == requestId) {
	  				this.mayI = requestId;
	  				return;
	  			}
	  		}
	  	}
	  	
	  	this.updateStatusCallMayI(util.getName(requestId));
	},
	
 	callMayI: function() {
	  if (this.turn != TURN_END) {
	  	  Ext.getCmp('mayIButton').toggle(false);
		  return;
	  }
	  
	  if (Ext.getCmp('mayIButton').pressed == true) {
		  util.submitMayIRequest();
		  this.updateStatusCallMayI(util.getName(util.getMyId()));
	  }
    },

	getRoundCriteria: function() {
	  var cardsLeft = this.getNumberOfCardsToDeal()-1;	
	  var runs = parseInt(cardsLeft/4);
	  while ((cardsLeft - runs*4)%3 != 0) {
		  runs--;
	  }
	  var groups = (cardsLeft - runs*4)/3;
	  	  
	  var round = {};
	  round['runs'] = runs;
	  round['groups'] = groups;
	  	  
	  return round;
  	},
  	
  	isGameEnd: function() {
  		return((this.round+1) == NUM_ROUNDS); 
  	},

	getNumberOfCardsToDeal: function() {
		return START_CARDS + this.round;
	},
	
	getPlayAreas: function() {
		
	},
	
	getToolbarButtons: function() {
		var buttons = [];
		buttons.push(new Ext.Button({
		    id: 'mayIButton',
		    text:    'May I',
		    enableToggle: true,
		    allowDepress: false,
		    scope:   this,
		    toggleHandler: function(button, state) {
		      game.callMayI();
		    }
		}));
		
		return buttons;
	},
	
	splashOnly: function() {
		return((this.round+1) == NUM_ROUNDS); 
	},
	
	updateStatusCallMayI: function(name) {
	  var s = name+' called May I';
	  this.win.updateStatus(s);
  	},
  
  	updateStatusMayI: function(fromName, toName) {
	  	var s = toName+' did a May I. '+fromName+' drew a card from the deck.';
	  	this.win.updateStatus(s);
  	},
  	
  	giveMayICards: function() {
  		var card1 = this.discardpile.getTopCard();
  	    var card2 = this.unuseddeck.getTopCard();
  	     var opphand = this.hands[util.getOpponentIndex(this.mayI)];
  	       	     
  	     card1.hide();
  	     card1.move(opphand);
  	     var s = {};
		 s.from = 'discardPile';
		 s.card = card1.toObject();
		 util.submitMayICards(this.mayI, s);
  	       	     
  	     card2.move(opphand);
  	     s = {};
		 s.from = 'unuseddeck';
		 s.card = card2.toObject();
		 util.submitMayICards(this.mayI, s);
  	},
  	
  	createPlayareas: function() {
  		console.log('Creating Playareas');
  		
  		this.playareas = [];
		this.playareas[0] = [];
		
		var round = this.getRoundCriteria();
		var runs = parseInt(round['runs']);
		var groups = parseInt(round['groups']);
			
		var i = 0;
			
		for (i = 0; i < runs; i++) {
			this.playareas[0][i] = new ExtGames.MayIPlayArea({
				itemId: 'playarea_run_' + i,
				type: 0,
				opp: 0,
				listeners: {
		            'carddropped': {
		            	fn: this.dropCard,
		            	scope: this,
		            }
		          }
		        });  
		}
		
		for (; i < (groups+runs); i++) {
			this.playareas[0][i] = new ExtGames.MayIPlayArea({
				itemId: 'playarea_group_' + i,
				type: 1,
				opp: 0,
				listeners: {
		            'carddropped': {
		            	fn: this.dropCard,
		            	scope: this,
		            }
		          }
		        });  
		}
		
		var opp = util.getOpponents();
			
		for (var j = 0; j < opp.length; j++) {
			this.playareas[j+1] = [];
			for (i = 0; i < runs; i++) {
				this.playareas[j+1][i] = new ExtGames.MayIPlayArea({
					itemId: 'playarea_'+j+'_run_' + i,
					type: 0,
					opp: 1,
					listeners: {
			            'carddropped': {
			            	fn: this.dropCard,
			            	scope: this,
			            }
			          }
			        });  
			}
			
			for (; i < (groups+runs); i++) {
				this.playareas[j+1][i] = new ExtGames.MayIPlayArea({
					itemId: 'playarea_'+j+'_group_' + i,
					type: 1,
					opp: 1,
					listeners: {
			            'carddropped': {
			            	fn: this.dropCard,
			            	scope: this,
			            }
			          }
			        });  
			}
		}
		
		console.log('Created Playareas');
  
  	}

});